const { subjectsModel } = require('../models/subjectsModel');

module.exports.studentsModel = async function (req, res) {

    let result = await studentsModel.find();
    res.send({
        massage: '学生数据获取成功',
        code: 1,
        result
    })
}

module.exports.createSubjects = async function (req, res) {

    try {
        const data = req.body;
        const result = await subjectsModel.create(data);
        if (result) {
            res.send({
                massage: '专业数据新增成功',
                code: 1
            })
        }
    } catch (error) {
        console.log('垃圾服务器');
        res.send({
            massage: '专业数据新增失败',
            code: 0
        })
    }
}
module.exports.deleteSubjects = async function (req, res) {

    try {
        const data = req.body;
        const result = await subjectsModel.create(data);
        if (result) {
            res.send({
                massage: '专业数据新增成功',
                code: 1
            })
        }
    } catch (error) {
        console.log('垃圾服务器');
        res.send({
            massage: '专业数据新增失败',
            code: 0
        })
    }
}

module.exports.getSubjects = async function (req, res) {
    const {currentPage, pageSize , type = 'name', value = '' } = req.query
    const total = await subjectsModel.countDocuments({ [type]: { $regex: value, $options: '$i' } }) //数据总数
    let pages = 1;
    if (pageSize) {

        pages = Math.ceil(total / pageSize)
    }
    const result = await subjectsModel
    .find({ [type]: { $regex: value, $options: '$i' } })
    // .populate('subjectsId classesId')
    .limit(pageSize - 0)
    .skip((currentPage - 1) * pageSize)
    res.send({
        massage: '专业数据获取成功',
        code: 1,
        result
    })
}